package com.changfang.gold_man.controller;


import com.changfang.gold_man.common.ImageUtil;
import com.changfang.gold_man.common.ResCode;
import com.changfang.gold_man.common.ResultVO;
import com.changfang.gold_man.service.Imp.CourierServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/courier")
@CrossOrigin(value = "*",maxAge = 1800,allowedHeaders = "*")

public class CourierController {
    @Autowired
    private CourierServiceImpl courierServiceImpl;
    //ID查快递
    @GetMapping("/findUid")
    public ResultVO findUid(@RequestParam String user_id) {
        try {
            return new ResultVO(ResCode.SUCCESS, courierServiceImpl.findUid(user_id));
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, e);
        }
    }
    //查询所有快递
    @GetMapping("/all")
    public ResultVO findAll() {
        try {
            return new ResultVO(ResCode.SUCCESS, courierServiceImpl.findAll());
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, e);
        }
    }
    //修改状态
    @GetMapping("/updateStatus")
    public ResultVO updateStatus(@RequestParam String courier_id,@RequestParam String courier_status) {
        try {
            return new ResultVO(ResCode.SUCCESS, courierServiceImpl.updateStatus(courier_id,courier_status));
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, e);
        }
    }
    //删除快递
    @GetMapping("/delete")
    public ResultVO delete(@RequestParam String courier_id) {
        try {
            return new ResultVO(ResCode.SUCCESS, courierServiceImpl.delete(courier_id));
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, e);
        }
    }
    //导入快递
    @PostMapping("/upload")
    public ResultVO upload(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        if(!file.isEmpty()){
            String img = ImageUtil.fileUpload(file);
            String courier_id = request.getParameter("courier_id");
            String courier_title = request.getParameter("courier_title");
            String courier_address = request.getParameter("courier_address");
            String courier_code = request.getParameter("courier_code");
            String user_id = request.getParameter("user_id");
            Map<String,String> courierMap = new HashMap<>();
            courierMap.put("courier_id",courier_id);
            courierMap.put("courier_title",courier_title);
            courierMap.put("courier_address",courier_address);
            courierMap.put("courier_code",courier_code);
            courierMap.put("user_id",user_id);
            courierServiceImpl.addCourier(courierMap,img);
            return new ResultVO(ResCode.SUCCESS, "导入快递信息成功");
        }
        return new ResultVO(ResCode.ERROR, "导入快递信息失败!");
    }
}
